<?php
namespace app\decorate\controller;
use app\admin\controller\Admin;
use app\AdminRest;
use app\ApiRest;
use app\decorate\model\Config;
use app\decorate\model\StaffCase;
use think\App;
use think\facade\Db;
use think\Request;
use app\decorate\model\IndexCase as case_model;
use app\decorate\model\AdminProgress;
use app\decorate\model\AdminStyle;
use app\decorate\model\AdminArea;
use app\decorate\model\AdminHousewear;
use app\decorate\model\AdminPrice;
use app\decorate\model\AdminMeasure;
use app\decorate\model\IndexSiteIv;
use app\decorate\model\AdminDynamic;
use app\decorate\model\IndexCollect;


class IndexCase extends ApiRest{
    protected $model;
    protected $progress_model;
    protected $style_model;
    protected $area_model;
    protected $house_wear_model;
    protected $price_model;
    protected $m_model;
    protected $iv_model;
    protected $dyn_model;
    protected $collect_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new case_model();
        $this->progress_model  = new AdminProgress();
        $this->style_model     = new AdminStyle();
        $this->area_model      = new AdminArea();
        $this->house_wear_model= new AdminHousewear();
        $this->price_model     = new AdminPrice();
        $this->m_model         = new AdminMeasure();
        $this->iv_model        = new IndexSiteIv();
        $this->dyn_model       = new AdminDynamic();
        $this->collect_model   = new IndexCollect();

    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:案列列表
     */
    public function caseList(){

        $input = $this->_input;

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        if($config['case_show']==1){

            $staff_id = !empty($input['staff_id'])?$input['staff_id']:0;

            $case_id = $this->model->staffCaseShow($staff_id,$this->_uniacid,$config['case_show_num']);

            $dis[] = ['id','in',$case_id];

        }

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['name'])){

            $dis[] = ['title','like',"%\\".$input['name']."%"];

        }
        if(!empty($input['housewear'])){
            $input['housewear'] = array_column($input['housewear'],'id');
            $dis[] = ['housewear','in',$input['housewear']];
        }
        if(!empty($input['progress'])){
            $input['progress'] = array_column($input['progress'],'id');
            $dis[] = ['progress','in',$input['progress']];
        }
        if(!empty($input['area'])){

            $area = $this->area_model->resetV2([$input['area']]);

            $dis[] = ['area','in',$area];
        }

        if(!empty($input['style'])){

            $input['style'] = array_column($input['style'],'id');

            $dis[] = ['style','in',$input['style']];

        }

        if(isset($input['is_vr'])&&$input['is_vr']!=2){

            $dis[] = ['is_vr','=',$input['is_vr']];
        }

        $dis_price = array();

        if(!empty($input['price'])){

            foreach ($input['price'] as $value){

                $min = $value['min'];

                $max = $value['max'];

                $dis_price[] = ['total_price','between',"$min,$max"];
            }

        }

        $dis_m = array();
        if(!empty($input['m'])){
            foreach ($input['m'] as $value){
                $min = $value['min'];
                $max = $value['max'];
                $dis_m[] = ['m','between',"$min,$max"];
            }
        }
        $sort = !empty($input['sort'])?$input['sort']:0;

        $data= $this->model->caseList($dis,10,$dis_price,$dis_m,$sort);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-16 17:25
     * @功能说明:员工案例列表
     */
    public function staffCaseList(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['title','like','%'.$input['name'].'%'];
        }

        $data = $this->model->where($dis)->where($where)->order('sort desc')->paginate(10)->toArray();

        if(!empty($data['data'])){

            $staff_case_model = new StaffCase();

            foreach ($data['data'] as &$v){

                $dis = [

                    'user_id' => $this->getUserId(),

                    'case_id'  => $v['id']
                ];

                $info = $staff_case_model->where($dis)->find();

                $v['is_select'] = !empty($info)?1:0;
            }

        }
        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-17 09:41
     * @功能说明:员工选择案例
     */
    public function selectCase(){

        $input = $this->_input;

        $staff_case_model = new StaffCase();

        $dis = [

            'user_id' => $this->getUserId(),

            'case_id'  => $input['case_id']
        ];

        $info = $staff_case_model->where($dis)->find();

        if(empty($info)){

            $config_model = new Config();
            //限制数量
            $limit_num  = $config_model->where(['uniacid'=>$this->_uniacid])->value('case_show_num');
            //已选数量
            $select_num = $staff_case_model->haveSelectNum($this->getUserId());

            if($limit_num<=$select_num){

                $this->errorMsg('超过限制数量');
            }

            $insert = [

                'uniacid' => $this->_uniacid,

                'user_id' => $this->getUserId(),

                'case_id' => $input['case_id']
            ];

            $staff_case_model->insert($insert);

        }else{

            $staff_case_model->where($dis)->delete();

        }

        return $this->success(true);

    }

    /**
     * User: chenniang
     * Date: 2019-10-17 17:14
     * @return void
     * descption:案列首页
     */
    public function caseIndex(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',1];

        $data['area']     = $this->area_model->qareaSelect($dis);
        $data['progress'] = $this->progress_model->progressSelect($dis);
        $data['housewear']= $this->house_wear_model->housewearSelect($dis);
        $data['price']    = $this->price_model->priceSelect($dis);
        $data['measure']  = $this->m_model->mselect($dis);
        $data['style']    = $this->style_model->styleSelect($dis);

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        if($config['case_show']==1){

            $staff_id = !empty($input['staff_id'])?$input['staff_id']:0;

            $case_id = $this->model->staffCaseShow($staff_id,$this->_uniacid,$config['case_show_num']);

            $dis[] = ['id','in',$case_id];

        }

        $data['site']  = $this->model->caseList($dis);

        return $this->success($data);
    }
    /**
     * User: chenniang
     * Date: 2019-10-17 17:55
     * @return void
     * descption:案列详情
     */
    public function caseInfo(){
        $input = $this->_param;
        $this->model->where(['id'=>$input['id']])->update(['iv'=>Db::raw('iv+1')]);
        $data['info'] = $this->model->caseInfo(['id'=>$input['id']]);
        if(empty($data['info'])){
            $this->collect_model->collectDel($this->getUserId(),$input['id'],1);
            return $this->error('not data');
        }
        $collect      = $this->collect_model->collectInfo(['user_id'=>$this->getUserId(),'type'=>1,'news_id'=>$data['info']['id'],'status'=>1]);
        $data['info']['is_collect'] = !empty($collect)?1:0;
        $r_dis[] = ['uniacid','=',$this->_uniacid];
        $r_dis[] = ['style','=',$data['info']['style']];
        $r_dis[] = ['recommend','=',1];
        $r_dis[] = ['id','<>',$data['info']['id']];
        $data['recommend_case'] = $this->model->caseSelect($r_dis);

        $data['staff_phone']    = Db::name('longbing_card_user_info')->where(['fans_id'=>$input['staff_id']])->value('phone');

        $site = lbPulgSettingInfo($this->_uniacid,'decorate',['offer_switch']);

        $data['offer_switch'] = $site['offer_switch'];

        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-17 19:13
     * @return \think\Response
     * descption:动态列表
     */
    public function dynList(){
        $input = $this->_input;
        $data  = $this->dyn_model->dynList(['uniacid'=>$this->_uniacid,'site_id'=>$input['id'],'status'=>1]);
        return $this->success($data);
    }


    /**
     * User: chenniang
     * Date: 2019-10-29 10:34
     * @return void
     * descption:公司简介
     */
    public function companyDes(){
        $input = $this->_input;
        $key   = !empty($input['key'])?$input['key']:[];
        $data  = lbPulgSettingInfo($this->_uniacid,'decorate',$key);
        return $this->success($data);
    }


    public function diylist()
    {
        $param = $this->_param;
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['status', '=', 1];
        if (!empty($param['keyword'])) {
            $filter[] = ['title', 'like', '%' . $param['keyword'] . '%' ];
        }
        $list = $this->model->where($filter)
            ->paginate(['list_rows' => $param['page_count'] ? $param['page_count'] : 10, 'page' => $param['page'] ? $param['page'] : 1])->toArray();

        return $this->success($list);
    }

    /**
     * @author chenniang
     * @DataTime: 2021-01-06 18:03
     * @功能说明:配置详情
     */
    public function configInfo(){

        $config_model = new Config();

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $data = $config_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-16 14:39
     * @功能说明:推荐案例
     */
    public function showCashList(){



    }
}
